#include <unordered_set>
#include <algorithm>
using namespace std;
class Solution {
    unordered_set<int> s;
public:
    int longestConsecutive(vector<int> &num) {
        s.clear();
        for (int i=0;i<num.size();i++) s.insert(num[i]);
        int ret=0;
        for (int i=0;i<num.size();i++) {
            int tmp=0;
            unordered_set<int>::iterator it=s.find(num[i]);
            if (it!=s.end()) {
                tmp=1;
                s.erase(it);
                for (int j=-1;;--j) {
                    it=s.find(num[i]+j);
                    if (it!=s.end()) {
                        s.erase(it);
                        ++tmp;
                    } else break;
                }
                for (int j=1;;++j) {
                    it=s.find(num[i]+j);
                    if (it!=s.end()) {
                        s.erase(it);
                        ++tmp;
                    } else break;
                }
            }
            ret=max(ret, tmp);
        }
        return ret;
    }
};
